package cn.iocoder.yudao.module.member.controller.admin.usercommission;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.usercommission.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usercommission.UserCommissionDO;
import cn.iocoder.yudao.module.member.service.usercommission.UserCommissionService;

@Tag(name = "管理后台 - 佣金")
@RestController
@RequestMapping("/member/user-commission")
@Validated
public class UserCommissionController {

    @Resource
    private UserCommissionService userCommissionService;

    @PostMapping("/create")
    @Operation(summary = "创建佣金")
    @PreAuthorize("@ss.hasPermission('member:user-commission:create')")
    public CommonResult<Long> createUserCommission(@Valid @RequestBody UserCommissionSaveReqVO createReqVO) {
        return success(userCommissionService.createUserCommission(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新佣金")
    @PreAuthorize("@ss.hasPermission('member:user-commission:update')")
    public CommonResult<Boolean> updateUserCommission(@Valid @RequestBody UserCommissionSaveReqVO updateReqVO) {
        userCommissionService.updateUserCommission(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除佣金")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-commission:delete')")
    public CommonResult<Boolean> deleteUserCommission(@RequestParam("id") Long id) {
        userCommissionService.deleteUserCommission(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得佣金")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-commission:query')")
    public CommonResult<UserCommissionRespVO> getUserCommission(@RequestParam("id") Long id) {
        UserCommissionDO userCommission = userCommissionService.getUserCommission(id);
        return success(BeanUtils.toBean(userCommission, UserCommissionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得佣金分页")
    @PreAuthorize("@ss.hasPermission('member:user-commission:query')")
    public CommonResult<PageResult<UserCommissionRespVO>> getUserCommissionPage(@Valid UserCommissionPageReqVO pageReqVO) {
        PageResult<UserCommissionDO> pageResult = userCommissionService.getUserCommissionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserCommissionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出佣金 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-commission:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCommissionExcel(@Valid UserCommissionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserCommissionDO> list = userCommissionService.getUserCommissionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "佣金.xls", "数据", UserCommissionRespVO.class,
                        BeanUtils.toBean(list, UserCommissionRespVO.class));
    }

}